<!DOCTYPE html>
<html>
<head>
<title>Molecular Beam Apparatus</title>
</head>
<body>
<canvas id="myCanvas" width="600" height="450" style="border:1px solid #d3d3d3;"></canvas>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");

// --- Style and Font Setup ---
ctx.lineWidth = 2;
ctx.strokeStyle = "black";
ctx.fillStyle = "black";
ctx.font = "20px sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";

// --- Coordinates and Dimensions ---
const ovenX = 50;
const ovenY = 175;
const ovenSize = 100;
const chamberLeft = ovenX + ovenSize; // 150
const chamberTop = 100;
const chamberRight = 550;
const chamberBottom = 350;
const beamCenterY = ovenY + ovenSize / 2; // 225

// --- Draw Oven ---
ctx.beginPath();
ctx.rect(ovenX, ovenY, ovenSize, ovenSize);
ctx.stroke();
ctx.fillText("oven", ovenX + ovenSize / 2, beamCenterY);

// --- Draw Vacuum Chamber ---
const pumpOutletWidth = 50;
const pumpOutletDepth = 25;
const pumpOutletX = 425; // center of outlet
const pumpOutletTop = chamberBottom;
const pumpOutletBottom = pumpOutletTop + pumpOutletDepth;

ctx.beginPath();
ctx.moveTo(chamberLeft, chamberTop); // Top-left
ctx.lineTo(chamberRight, chamberTop); // Top edge
ctx.lineTo(chamberRight, chamberBottom); // Right edge
ctx.lineTo(pumpOutletX + pumpOutletWidth / 2, chamberBottom); // Bottom edge to pump
ctx.lineTo(pumpOutletX + pumpOutletWidth / 2, pumpOutletBottom); // Pump right side
ctx.moveTo(pumpOutletX - pumpOutletWidth / 2, pumpOutletBottom); // Pump left side (move to avoid a line across)
ctx.lineTo(pumpOutletX - pumpOutletWidth / 2, chamberBottom); // Pump left side
ctx.lineTo(chamberLeft, chamberBottom); // Bottom edge to left
ctx.lineTo(chamberLeft, ovenY + ovenSize); // Left wall, bottom part (aligns with oven bottom)
ctx.moveTo(chamberLeft, ovenY); // Left wall, top part (aligns with oven top)
ctx.lineTo(chamberLeft, chamberTop); // Connect to chamber top
ctx.stroke();

// --- Draw Pump Outlet details ---
const arrowX = pumpOutletX;
const arrowY_start = pumpOutletBottom;
const arrowY_end = arrowY_start + 25;
ctx.beginPath();
ctx.moveTo(arrowX, arrowY_start);
ctx.lineTo(arrowX, arrowY_end);
// Arrowhead
ctx.moveTo(arrowX, arrowY_end);
ctx.lineTo(arrowX - 6, arrowY_end - 8);
ctx.moveTo(arrowX, arrowY_end);
ctx.lineTo(arrowX + 6, arrowY_end - 8);
ctx.stroke();

// Pump label
ctx.textAlign = "left";
ctx.fillText("to pump", arrowX - pumpOutletWidth / 2, arrowY_end + 10);

// --- Draw Collimating Slits ---
ctx.textAlign = "center"; // Reset alignment
const slit1X = 250;
const slit2X = 320;
const slitHeight = 160;
const slitGap = 20;
const slitTopY = beamCenterY - slitHeight / 2;
const slitBottomY = beamCenterY + slitHeight / 2;
const slitGapTopY = beamCenterY - slitGap / 2;
const slitGapBottomY = beamCenterY + slitGap / 2;

// Slit 1
ctx.beginPath();
ctx.moveTo(slit1X, slitTopY);
ctx.lineTo(slit1X, slitGapTopY);
ctx.moveTo(slit1X, slitGapBottomY);
ctx.lineTo(slit1X, slitBottomY);
ctx.stroke();

// Slit 2
ctx.beginPath();
ctx.moveTo(slit2X, slitTopY);
ctx.lineTo(slit2X, slitGapTopY);
ctx.moveTo(slit2X, slitGapBottomY);
ctx.lineTo(slit2X, slitBottomY);
ctx.stroke();

// Slits Label
ctx.fillText("collimating slits", (slit1X + slit2X) / 2, slitTopY - 20);

// --- Draw Molecular Beam (Dashed Lines) ---
ctx.save();
ctx.setLineDash([7, 5]);
const beamStartX = chamberLeft;
const beamEndX = chamberRight - 30; // 520
const beamY_offset = 5;
const beamDivergence = 8;

// Top dashed line
ctx.beginPath();
ctx.moveTo(beamStartX, beamCenterY - beamY_offset);
ctx.lineTo(slit2X, beamCenterY - beamY_offset); // Parallel part
ctx.lineTo(beamEndX, beamCenterY - beamY_offset - beamDivergence); // Diverging part
ctx.stroke();

// Bottom dashed line
ctx.beginPath();
ctx.moveTo(beamStartX, beamCenterY + beamY_offset);
ctx.lineTo(slit2X, beamCenterY + beamY_offset); // Parallel part
ctx.lineTo(beamEndX, beamCenterY + beamY_offset + beamDivergence); // Diverging part
ctx.stroke();
ctx.restore();

// --- Draw other Labels ---
ctx.fillText("vacuum", 400, 310);

// Figure Caption
ctx.font = "20px 'Times New Roman'";
ctx.fillText("Fig. 2.40.", 300, 430);

</script>
</body>
</html>